package com.group.flagtivities;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class WebUtil {
	
	private static final String root = "http://flagtivities.netau.net/";
	
	private static String queryDatabase(final String url, final ArrayList<NameValuePair> params) {
		String result;
		try{
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost(root + url);
			if (params != null) post.setEntity(new UrlEncodedFormEntity(params));
			HttpResponse response = client.execute(post);
			HttpEntity entity = response.getEntity();
			InputStream is = entity.getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();
			result = sb.toString();
		}catch(Exception e) {
			return null;
		}
		return result.trim();
	}
	
	public static String setPassword(final int user_id, final String passwordOld, final String passwordNew) {
		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("user_id", ""+user_id));
		params.add(new BasicNameValuePair("password_old", passwordOld));
		params.add(new BasicNameValuePair("password_new", passwordNew));
		return queryDatabase("setPassword.php", params);
	}
	
	public static String login(final String username, final String password) {
		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("username", username));
		params.add(new BasicNameValuePair("password", password));
		return queryDatabase("login.php", params);
	}
	
	public static String setProfilePicture(final String profile_picture, final int user_id) {
		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("profile_picture", profile_picture));
		params.add(new BasicNameValuePair("user_id", ""+user_id));
		return queryDatabase("setProfilePicture.php", params);
	}
	
	public static String retrieveActivities() {
		return queryDatabase("retrieveActivities.php", null);
	}
	
	public static String retrieveFriends(final int user_id) {
		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("user_id", ""+user_id));
		return queryDatabase("retrieveFriends.php", params);
	}
	
	public static String requestFriend(final int user_id, final int friend_id) {
		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("user_id", ""+user_id));
		params.add(new BasicNameValuePair("friend_id", ""+friend_id));
		return queryDatabase("requestFriend.php", params);
	}
	
	public static String addFriend(final int user_id, final int friend_id) {
		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("user_id", ""+user_id));
		params.add(new BasicNameValuePair("friend_id", ""+friend_id));
		return queryDatabase("addFriend.php", params);
	}

	public static String unFriend(final int user_id, final int friend_id) {
		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("user_id", ""+user_id));
		params.add(new BasicNameValuePair("friend_id", ""+friend_id));
		return queryDatabase("unFriend.php", params);
	}
	
	public static String removeFriend(final int user_id, final int friend_id) {
		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("user_id", ""+user_id));
		params.add(new BasicNameValuePair("friend_id", ""+friend_id));
		return queryDatabase("removeFriend.php", params);
	}
	
	public static String searchFriends(final String search_criteria) {
		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("search_criteria", ""+search_criteria));
		return queryDatabase("searchFriends.php", params);
	}
	
	public static String createActivity(final int permission, final int type, final int owner, final String description, final int lat, final int lon, final String room, final String title, final String start, final String end) {
		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("permission", ""+permission));
		params.add(new BasicNameValuePair("type", ""+type));
		params.add(new BasicNameValuePair("owner", ""+owner));
		params.add(new BasicNameValuePair("description", description));
		params.add(new BasicNameValuePair("lat", ""+lat));
		params.add(new BasicNameValuePair("lon", ""+lon));
		params.add(new BasicNameValuePair("room", room));
		params.add(new BasicNameValuePair("title", title));
		params.add(new BasicNameValuePair("start", start));
		params.add(new BasicNameValuePair("end", end));
		return queryDatabase("createActivity.php", params);
	}
		
	public static String retrieveFriendActivities(final int friend_id) {
		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("friend_id", ""+friend_id));
		return queryDatabase("retrieveFriendActivities.php", params);
	}
	
	public static String deleteActivity(final int activity_id) {
		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("activity_id", ""+activity_id));
		return queryDatabase("deleteActivity.php", params);
	}
	
	public static String dislikeActivity(final int activity_id) {
		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("activity_id", ""+activity_id));
		return queryDatabase("dislikeActivity.php", params);
	}
	
	public static String likeActivity(final int activity_id) {
		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("activity_id", ""+activity_id));
		return queryDatabase("likeActivity.php", params);
	}
	
	public static String RSVPActivity(final int activity_id) {
		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("activity_id", ""+activity_id));
		return queryDatabase("RSVP.php", params);
	}
	
	public static String unRSVPActivity(final int activity_id) {
		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("activity_id", ""+activity_id));
		return queryDatabase("unRSVP.php", params);
	}
	
}
